use serde::Deserialize;
use utoipa::ToSchema;
use uuid::Uuid;
use validator::Validate;

#[derive(Clone, Debug, Default, Deserialize, ToSchema, Validate)]
#[schema(title = "创建管理员用户", description = "创建管理员用户请求参数结构体")]
pub struct CreateManagerUserRequest {
    #[validate(length(
        min = 3,
        max = 30,
        message = "用户名必须是 3 到 30 个字符之间的字母、数字或下划线开头的字符串"
    ))]
    #[schema(example = "user123", required = true, min_length = 3, max_length = 30)]
    pub name: String,

    #[validate(length(
        min = 2,
        max = 50,
        message = "真实姓名必须是 2 到 50 个字符之间的字符串"
    ))]
    #[schema(example = "张三", required = true, min_length = 2, max_length = 50)]
    pub real_name: String,

    #[validate(length(min = 2, max = 30, message = "昵称必须是 2 到 30 个字符之间的字符串"))]
    #[schema(
        example = "小张",
        required = false,
        nullable = true,
        min_length = 2,
        max_length = 30
    )]
    pub nickname: Option<String>,

    #[validate(length(max = 30, message = "角色类型必须是小于 30 个字符的字符串"))]
    #[schema(example = "admin,manager", required = true, max_length = 3300)]
    pub role_types: String,

    #[validate(length(max = 30, message = "角色类型必须是小于 30 个字符的字符串"))]
    #[schema(
        example = "tester,developer",
        required = false,
        nullable = true,
        max_length = 30
    )]
    pub inner_types: Option<String>,

    #[validate(range(min = 0, max = 2, message = "性别必须是 0-2 之间的数字"))]
    #[schema(example = 1, required = true, default = 0, minimum = 0, maximum = 2)]
    pub gender: i16,

    #[validate(length(equal = 11, message = "手机号必须是 11 位数字"))]
    #[schema(
        example = "13800138000",
        required = false,
        nullable = true,
        min_length = 11,
        max_length = 11
    )]
    pub phone: Option<String>,

    #[validate(url(message = "头像路径必须是一个有效的 URL"))]
    #[schema(
        example = "https://example.com/avatar.jpg",
        required = false,
        nullable = true,
        format = "uri",
        value_type = String
    )]
    pub avatar_path: Option<String>,

    #[validate(email(message = "邮箱必须是一个有效的电子邮件地址"))]
    #[schema(
        example = "user@example.com",
        required = false,
        nullable = true,
        format = "email",
        value_type = String
    )]
    pub email: Option<String>,

    #[validate(length(min = 6, max = 30, message = "密码必须是 6 到 30 个字符之间的字符串"))]
    #[schema(
        example = "password123",
        required = true,
        nullable = true,
        min_length = 6,
        max_length = 30
    )]
    pub new_password: String,

    #[validate(must_match(other = "new_password", message = "确认密码必须与新密码字段匹配"))]
    #[schema(
        example = "password123",
        required = true,
        nullable = true,
        min_length = 6,
        max_length = 30
    )]
    pub confirm_password: String,
}

#[derive(Clone, Debug, Default, Deserialize, ToSchema, Validate)]
#[schema(
    title = "手机号和密码注册会员",
    description = "通过手机号和密码注册会员用户请求参数结构体"
)]
pub struct RegisterMemberUserByPhoneAndPasswordRequest {
    #[validate(length(
        min = 3,
        max = 30,
        message = "用户名必须是 3 到 30 个字符之间的字母、数字或下划线开头的字符串"
    ))]
    #[schema(
        example = "user123",
        required = false,
        nullable = true,
        min_length = 3,
        max_length = 30
    )]
    pub name: Option<String>,

    #[validate(length(
        min = 2,
        max = 50,
        message = "真实姓名必须是 2 到 50 个字符之间的字符串"
    ))]
    #[schema(
        example = "张三",
        required = false,
        nullable = true,
        min_length = 2,
        max_length = 50
    )]
    pub real_name: Option<String>,

    #[validate(length(min = 2, max = 30, message = "昵称必须是 2 到 30 个字符之间的字符串"))]
    #[schema(
        example = "小张",
        required = false,
        nullable = true,
        min_length = 2,
        max_length = 30
    )]
    pub nickname: Option<String>,

    #[validate(range(min = 0, max = 2, message = "性别必须是 0-2 之间的数字"))]
    #[schema(example = 1, required = true, default = 0, minimum = 0, maximum = 2)]
    pub gender: i16,

    #[validate(length(equal = 11, message = "手机号必须是 11 位数字"))]
    #[schema(
        example = "13800138000",
        required = true,
        nullable = false,
        min_length = 11,
        max_length = 11
    )]
    pub phone: String,

    #[validate(url(message = "头像路径必须是一个有效的 URL"))]
    #[schema(
        example = "https://example.com/avatar.jpg",
        required = false,
        nullable = true,
        format = "uri",
        value_type = String
    )]
    pub avatar_path: Option<String>,

    #[validate(email(message = "邮箱必须是一个有效的电子邮件地址"))]
    #[schema(
        example = "user@example.com",
        required = false,
        nullable = true,
        format = "email",
        value_type = String
    )]
    pub email: Option<String>,

    #[validate(required(message = "数据源 ID 必须是一个有效的 UUID"))]
    #[schema(
        example = "550e8400-e29b-41d4-a716-446655440000",
        required = true,
        nullable = true,
        format = "uuid",
        value_type = Uuid
    )]
    pub data_source_id: Option<Uuid>,

    #[validate(length(min = 6, max = 30, message = "密码必须是 6 到 30 个字符之间的字符串"))]
    #[schema(
        example = "password123",
        required = true,
        nullable = true,
        min_length = 6,
        max_length = 30
    )]
    pub new_password: String,

    #[validate(must_match(other = "new_password", message = "确认密码必须与新密码字段匹配"))]
    #[schema(
        example = "password123",
        required = true,
        nullable = true,
        min_length = 6,
        max_length = 30
    )]
    pub confirm_password: String,
}
